package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.SysAppBo;
import org.dromara.system.domain.vo.SysAppVo;
import org.dromara.system.domain.SysApp;
import org.dromara.system.mapper.SysAppMapper;
import org.dromara.system.service.ISysAppService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 应用Service业务层处理
 *
 * @author X
 * @date 2025-08-04
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class SysAppServiceImpl implements ISysAppService {

    private final SysAppMapper baseMapper;

    /**
     * 查询应用
     *
     * @param appId 主键
     * @return 应用
     */
    @Override
    public SysAppVo queryById(Long appId){
        return baseMapper.selectVoById(appId);
    }

    /**
     * 分页查询应用列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 应用分页列表
     */
    @Override
    public TableDataInfo<SysAppVo> queryPageList(SysAppBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysApp> lqw = buildQueryWrapper(bo);
        Page<SysAppVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的应用列表
     *
     * @param bo 查询条件
     * @return 应用列表
     */
    @Override
    public List<SysAppVo> queryList(SysAppBo bo) {
        LambdaQueryWrapper<SysApp> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysApp> buildQueryWrapper(SysAppBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysApp> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(SysApp::getAppId);
        lqw.eq(StringUtils.isNotBlank(bo.getAppCode()), SysApp::getAppCode, bo.getAppCode());
        lqw.like(StringUtils.isNotBlank(bo.getAppName()), SysApp::getAppName, bo.getAppName());
        lqw.eq(StringUtils.isNotBlank(bo.getAppLoginlogourl()), SysApp::getAppLoginlogourl, bo.getAppLoginlogourl());
        lqw.eq(StringUtils.isNotBlank(bo.getAppLoginpic()), SysApp::getAppLoginpic, bo.getAppLoginpic());
        lqw.eq(StringUtils.isNotBlank(bo.getAppLoginbgpic1()), SysApp::getAppLoginbgpic1, bo.getAppLoginbgpic1());
        lqw.eq(StringUtils.isNotBlank(bo.getAppLoginbgpic2()), SysApp::getAppLoginbgpic2, bo.getAppLoginbgpic2());
        lqw.eq(StringUtils.isNotBlank(bo.getAppLoginbgpic3()), SysApp::getAppLoginbgpic3, bo.getAppLoginbgpic3());
        lqw.eq(StringUtils.isNotBlank(bo.getAppLoginbgpic4()), SysApp::getAppLoginbgpic4, bo.getAppLoginbgpic4());
        lqw.eq(StringUtils.isNotBlank(bo.getAppLoginbgpic5()), SysApp::getAppLoginbgpic5, bo.getAppLoginbgpic5());
        lqw.eq(StringUtils.isNotBlank(bo.getAppLogourl()), SysApp::getAppLogourl, bo.getAppLogourl());
        lqw.eq(StringUtils.isNotBlank(bo.getAppLogoicourl()), SysApp::getAppLogoicourl, bo.getAppLogoicourl());
        lqw.eq(StringUtils.isNotBlank(bo.getCopyright()), SysApp::getCopyright, bo.getCopyright());
        lqw.eq(StringUtils.isNotBlank(bo.getDomain()), SysApp::getDomain, bo.getDomain());
        lqw.eq(StringUtils.isNotBlank(bo.getBeian()), SysApp::getBeian, bo.getBeian());
        lqw.eq(StringUtils.isNotBlank(bo.getDescription()), SysApp::getDescription, bo.getDescription());
        lqw.eq(StringUtils.isNotBlank(bo.getIndexpage()), SysApp::getIndexpage, bo.getIndexpage());
        lqw.eq(StringUtils.isNotBlank(bo.getMainpage()), SysApp::getMainpage, bo.getMainpage());
        lqw.eq(StringUtils.isNotBlank(bo.getKeywords()), SysApp::getKeywords, bo.getKeywords());
        lqw.eq(StringUtils.isNotBlank(bo.getSearchkeywords()), SysApp::getSearchkeywords, bo.getSearchkeywords());
        lqw.eq(StringUtils.isNotBlank(bo.getHelpurl()), SysApp::getHelpurl, bo.getHelpurl());
        lqw.eq(StringUtils.isNotBlank(bo.getPushrul()), SysApp::getPushrul, bo.getPushrul());
        lqw.eq(StringUtils.isNotBlank(bo.getAppTypecode()), SysApp::getAppTypecode, bo.getAppTypecode());
        lqw.eq(bo.getAppLevel() != null, SysApp::getAppLevel, bo.getAppLevel());
        return lqw;
    }

    /**
     * 新增应用
     *
     * @param bo 应用
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(SysAppBo bo) {
        SysApp add = MapstructUtils.convert(bo, SysApp.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setAppId(add.getAppId());
        }
        return flag;
    }

    /**
     * 修改应用
     *
     * @param bo 应用
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(SysAppBo bo) {
        SysApp update = MapstructUtils.convert(bo, SysApp.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysApp entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除应用信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }



}
